﻿using System;
using System.Collections.Generic;
using System.Web;

namespace Web
{
    public class SessionHelp
    {

        /// <summary>
        /// 获取登录用户
        /// </summary>
        /// <param name="page"></param>
        /// <returns></returns>
        public static fanli.Model.fanli_adminUsers GetLoginUser(System.Web.UI.Page page)
        {
            fanli.Model.fanli_adminUsers mod = new fanli.Model.fanli_adminUsers();
            mod.id = 1;
            mod.username = "admin";
            page.Session["LoginUser"] = mod;
           
            if (page.Session["LoginUser"] != null)
            {
                return (fanli.Model.fanli_adminUsers)page.Session["LoginUser"];
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 保存登录用户
        /// </summary>
        /// <param name="page"></param>
        /// <param name="loginUser"></param>
        public static void SetLoginUser(System.Web.UI.Page page, fanli.Model.fanli_adminUsers loginUser)
        {
            page.Session["LoginUser"] = loginUser;
        }
        /// <summary>
        /// 保证已登录，否则重定向到登录页面
        /// </summary>
        /// <param name="page"></param>
        public static void AssertLogin(System.Web.UI.Page page)
        {
            if (GetLoginUser(page) == null)
            {
                page.Response.Write("<script language='javascript'>top.location.href='/login.aspx';</script>");
                page.Response.End();
            }
        }
        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="page"></param>
        public static void LogOut(System.Web.UI.Page page)
        {
            page.Session.Clear();
            page.Response.Cookies["LoginUser"].Expires = DateTime.Now.AddDays(-1);
            page.Response.Write("<script language='javascript'>top.location.href='/Default.aspx';</script>");
            page.Response.End();
        }       
    }

}